home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP04.ZIP / CHAP04 / MALLOC / MALLOC.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  11KB  |  433 lines

  1. /*
  2.  * MALLOC.CPP
  3.  *
  4.  * Demostration of IMalloc object use.
  5.  *
  6.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Software Design Engineer
  9.  * Microsoft Systems Developer Relations
  10.  *
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include <windows.h>
  17. #include <ole2.h>
  18. #include <initguid.h>
  19. #include <ole2ver.h>
  20. #include "malloc.h"
  21.  
  22.  
  23. /*
  24.  * WinMain
  25.  *
  26.  * Purpose:
  27.  *  Main entry point of application.
  28.  */
  29.  
  30. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
  31.     , LPSTR pszCmdLine, int nCmdShow)
  32.     {
  33.     MSG         msg;
  34.     LPAPPVARS   pAV;
  35.  
  36.    #ifdef WIN32
  37.     //Recommended for all OLE 2.0 applications.
  38.     SetMessageQueue(96);
  39.    #endif
  40.  
  41.     //Create and initialize the application.
  42.     pAV=new CAppVars(hInst, hInstPrev, nCmdShow);
  43.  
  44.     if (NULL==pAV)
  45.         return -1;
  46.  
  47.     if (pAV->FInit())
  48.         {
  49.         while (GetMessage(&msg, NULL, 0,0 ))
  50.             {
  51.             TranslateMessage(&msg);
  52.             DispatchMessage(&msg);
  53.             }
  54.         }
  55.  
  56.     delete pAV;
  57.     return msg.wParam;
  58.     }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /*
  67.  * MallocWndProc
  68.  *
  69.  * Purpose:
  70.  *  Standard window class procedure.
  71.  */
  72.  
  73. LRESULT FAR PASCAL __export MallocWndProc(HWND hWnd, UINT iMsg
  74.     , WPARAM wParam, LPARAM lParam)
  75.     {
  76.     LPAPPVARS       pAV;
  77.     LPVOID          pv;
  78.     ULONG           cb;
  79.     UINT            i;
  80.     BOOL            fResult=TRUE;
  81.     HRESULT         hr;
  82.  
  83.     //This will be valid for all messages except WM_NCCREATE
  84.     pAV=(LPAPPVARS)GetWindowLong(hWnd, MALLOCWL_STRUCTURE);
  85.  
  86.     switch (iMsg)
  87.         {
  88.         case WM_NCCREATE:
  89.             //CreateWindow passed pAV to us.
  90.             pAV=(LPAPPVARS)((LONG)((LPCREATESTRUCT)lParam)->lpCreateParams);
  91.  
  92.             SetWindowLong(hWnd, MALLOCWL_STRUCTURE, (LONG)pAV);
  93.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  94.  
  95.         case WM_DESTROY:
  96.             PostQuitMessage(0);
  97.             break;
  98.  
  99.         case WM_COMMAND:
  100.             switch (LOWORD(wParam))
  101.                 {
  102.                 case IDM_IMALLOCCOGETMALLOCTASK:
  103.                     pAV->FreeAllocations(TRUE);
  104.  
  105.                     hr=CoGetMalloc(MEMCTX_TASK, &pAV->m_pIMalloc);
  106.                     fResult=SUCCEEDED(hr);
  107.  
  108.                     MessageBox(hWnd, ((fResult) ? "CoGetMalloc(task) succeeded."
  109.                         : "CoGetMalloc(task) failed."), "Malloc", MB_OK);
  110.  
  111.                     break;
  112.  
  113.  
  114.                 case IDM_IMALLOCCOGETMALLOCSHARED:
  115.                     pAV->FreeAllocations(TRUE);
  116.                     hr=CoGetMalloc(MEMCTX_SHARED, &pAV->m_pIMalloc);
  117.                     fResult=SUCCEEDED(hr);
  118.  
  119.                     MessageBox(hWnd, ((fResult) ? "CoGetMalloc(shared) succeeded."
  120.                         : "CoGetMalloc(shared) failed."), "Malloc", MB_OK);
  121.                     break;
  122.  
  123.  
  124.                 case IDM_IMALLOCRELEASE:
  125.                     pAV->FreeAllocations(TRUE);
  126.                     break;
  127.  
  128.  
  129.                 case IDM_IMALLOCALLOC:
  130.                     if (NULL==pAV->m_pIMalloc)
  131.                         break;
  132.  
  133.                     pAV->FreeAllocations(FALSE);
  134.  
  135.                     for (i=0; i < CALLOCS; i++)
  136.                         {
  137.                         LPBYTE    pb;
  138.                         ULONG     iByte;
  139.  
  140.                         cb=pAV->m_rgcb[i];
  141.                         pAV->m_rgpv[i]=pAV->m_pIMalloc->Alloc(cb);
  142.  
  143.                         //Fill the memory with letters.
  144.                         pb=(LPBYTE)pAV->m_rgpv[i];
  145.  
  146.                         if (NULL!=pb)
  147.                             {
  148.                             for (iByte=0; iByte < cb; iByte++)
  149.                                 *pb++=('a'+i);
  150.                             }
  151.  
  152.                         fResult &= (NULL!=pAV->m_rgpv[i]);
  153.                         }
  154.  
  155.                     MessageBox(hWnd, ((fResult) ? "IMalloc::Alloc succeeded."
  156.                         : "IMalloc::Alloc failed."), "Malloc", MB_OK);
  157.                     break;
  158.  
  159.  
  160.                 case IDM_IMALLOCFREE:
  161.                     pAV->FreeAllocations(FALSE);
  162.  
  163.                     MessageBox(hWnd, "IMalloc::Free finished.", "Malloc", MB_OK);
  164.                     break;
  165.  
  166.  
  167.                 case IDM_IMALLOCREALLOC:
  168.                     if (NULL==pAV->m_pIMalloc)
  169.                         break;
  170.  
  171.                     for (i=0; i < CALLOCS; i++)
  172.                         {
  173.                         LPBYTE      pb;
  174.                         ULONG       iByte;
  175.  
  176.                         pAV->m_rgcb[i]+=128;
  177.  
  178.                         //Old memory is not freed is Realloc fails here.
  179.                         pv=pAV->m_pIMalloc->Realloc(pAV->m_rgpv[i]
  180.                             , pAV->m_rgcb[i]);
  181.  
  182.                         if (NULL!=pv)
  183.                             {
  184.                             pAV->m_rgpv[i]=pv;
  185.  
  186.                             //Fill the new memory with something we can see.
  187.                             pb=(LPBYTE)pAV->m_rgpv[i];
  188.                             cb=pAV->m_rgcb[i];
  189.  
  190.                             if (NULL!=pb)
  191.                                 {
  192.                                 for (iByte=cb-128; iByte < cb; iByte++)
  193.                                     *pb++=('a'+i);
  194.                                 }
  195.                             }
  196.                         else
  197.                             fResult=FALSE;
  198.                         }
  199.  
  200.                     MessageBox(hWnd, ((fResult) ? "IMalloc::Realloc succeeded."
  201.                         : "IMalloc::Realloc failed."), "Malloc", MB_OK);
  202.  
  203.  
  204.                     break;
  205.  
  206.  
  207.                 case IDM_IMALLOCGETSIZE:
  208.                     if (NULL==pAV->m_pIMalloc)
  209.                         break;
  210.  
  211.                     for (i=0; i < CALLOCS; i++)
  212.                         {
  213.                         cb=pAV->m_pIMalloc->GetSize(pAV->m_rgpv[i]);
  214.  
  215.                         //We test that the size is *at least* what we wanted.
  216.                         fResult &= (pAV->m_rgcb[i] <= cb);
  217.                         }
  218.  
  219.                     MessageBox(hWnd, ((fResult) ? "IMalloc::GetSize matched."
  220.                         : "IMalloc::GetSize mismatch."), "Malloc", MB_OK);
  221.  
  222.                     break;
  223.  
  224.  
  225.                 case IDM_IMALLOCDIDALLOC:
  226.                     if (NULL==pAV->m_pIMalloc)
  227.                         break;
  228.  
  229.                     /*
  230.                      * DidAlloc may return -1 if it does not know whether
  231.                      * or not it actually allocated something.  In that
  232.                      * case we just blindly & in a -1 with no affect.
  233.                      */
  234.                     for (i=0; i < CALLOCS; i++)
  235.                         fResult &= pAV->m_pIMalloc->DidAlloc(pAV->m_rgpv[i]);
  236.  
  237.                     MessageBox(hWnd, ((fResult) ? "IMalloc::DidAlloc is TRUE."
  238.                         : "IMalloc::DidAlloc is FALSE."), "Malloc", MB_OK);
  239.  
  240.                     break;
  241.  
  242.  
  243.                 case IDM_IMALLOCHEAPMINIMIZE:
  244.                     if (NULL!=pAV->m_pIMalloc)
  245.                         pAV->m_pIMalloc->HeapMinimize();
  246.  
  247.                     MessageBox(hWnd, "IMalloc::HeapMinimize finished."
  248.                         , "Malloc", MB_OK);
  249.  
  250.                     break;
  251.  
  252.  
  253.                 case IDM_IMALLOCEXIT:
  254.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  255.                     break;
  256.                 }
  257.             break;
  258.  
  259.         default:
  260.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  261.         }
  262.  
  263.     return 0L;
  264.     }
  265.  
  266.  
  267.  
  268.  
  269.  
  270. /*
  271.  * CAppVars::CAppVars
  272.  * CAppVars::~CAppVars
  273.  *
  274.  * Constructor Parameters: (from WinMain)
  275.  *  hInst           HINSTANCE of the application.
  276.  *  hInstPrev       HINSTANCE of a previous instance.
  277.  *  nCmdShow        UINT specifying how to show the app window.
  278.  */
  279.  
  280. CAppVars::CAppVars(HINSTANCE hInst, HINSTANCE hInstPrev, UINT nCmdShow)
  281.     {
  282.     UINT        i;
  283.     ULONG       cb;
  284.  
  285.     m_hInst       =hInst;
  286.     m_hInstPrev   =hInstPrev;
  287.     m_nCmdShow    =nCmdShow;
  288.  
  289.     m_hWnd        =NULL;
  290.     m_pIMalloc    =NULL;
  291.     m_fInitialized=FALSE;
  292.  
  293.  
  294.     /*
  295.      * 100 is arbitrary--the task IMalloc can handle in indivudal allocs
  296.      * from 0 to 65535 bytes.  The shared IMalloc can handle larger size
  297.      * since it uses GlobalAlloc.
  298.      */
  299.  
  300.     cb=100;
  301.  
  302.     for (i=0; i < CALLOCS; i++)
  303.         {
  304.         m_rgcb[i]=cb;
  305.         m_rgpv[i]=NULL;
  306.  
  307.         cb*=2;
  308.         }
  309.  
  310.     return;
  311.     }
  312.  
  313.  
  314.  
  315. CAppVars::~CAppVars(void)
  316.     {
  317.     FreeAllocations(TRUE);
  318.  
  319.     if (m_fInitialized)
  320.         CoUninitialize();
  321.  
  322.     return;
  323.     }
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331. /*
  332.  * CAppVars::FInit
  333.  *
  334.  * Purpose:
  335.  *  Initializes an CAppVars object by registering window classes,
  336.  *  creating the main window, and doing anything else prone to failure
  337.  *  such as calling CoInitialize.  If this function fails the caller
  338.  *  should insure that the destructor is called.
  339.  *
  340.  * Parameters:
  341.  *  None
  342.  *
  343.  * Return Value:
  344.  *  BOOL            TRUE if successful, FALSE otherwise.
  345.  */
  346.  
  347. BOOL CAppVars::FInit(void)
  348.     {
  349.     WNDCLASS    wc;
  350.     DWORD       dwVer;
  351.  
  352.     //Make sure COMPOBJ.DLL is the right version
  353.     dwVer=CoBuildVersion();
  354.  
  355.     if (rmm!=HIWORD(dwVer))
  356.         return FALSE;
  357.  
  358.     //Call CoInitialize so we can call other Co* functions
  359.     if (FAILED(CoInitialize(NULL)))
  360.         return FALSE;
  361.  
  362.     m_fInitialized=TRUE;
  363.  
  364.     //Register our window classes.
  365.     if (!m_hInstPrev)
  366.         {
  367.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  368.         wc.lpfnWndProc    = MallocWndProc;
  369.         wc.cbClsExtra     = 0;
  370.         wc.cbWndExtra     = CBWNDEXTRA;
  371.         wc.hInstance      = m_hInst;
  372.         wc.hIcon          = LoadIcon(m_hInst, "Icon");
  373.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  374.         wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  375.         wc.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU);
  376.         wc.lpszClassName  = "MALLOC";
  377.  
  378.         if (!RegisterClass(&wc))
  379.             return FALSE;
  380.         }
  381.  
  382.     //Create the main window.
  383.     m_hWnd=CreateWindow("MALLOC", "IMalloc Object Demo"
  384.         , WS_OVERLAPPEDWINDOW, 35, 35, 350, 250, NULL, NULL, m_hInst, this);
  385.  
  386.     if (NULL==m_hWnd)
  387.         return FALSE;
  388.  
  389.     ShowWindow(m_hWnd, m_nCmdShow);
  390.     UpdateWindow(m_hWnd);
  391.  
  392.     return TRUE;
  393.     }
  394.  
  395.  
  396.  
  397. /*
  398.  * CAppVars::FreeAllocations
  399.  *
  400.  * Purpose:
  401.  *  Centralized place to clean up allocations made on the current IMalloc.
  402.  *
  403.  * Parameters:
  404.  *  fRelease        BOOL indicating if we're to IMalloc::Release as well.
  405.  *
  406.  * Return Value:
  407.  *  None
  408.  */
  409.  
  410. void CAppVars::FreeAllocations(BOOL fRelease)
  411.     {
  412.     UINT    i;
  413.  
  414.     if (NULL==m_pIMalloc)
  415.         return;
  416.  
  417.     for (i=0; i < CALLOCS; i++)
  418.         {
  419.         if (NULL!=m_rgpv[i])
  420.             m_pIMalloc->Free(m_rgpv[i]);
  421.  
  422.         m_rgpv[i]=NULL;
  423.         }
  424.  
  425.     if (fRelease)
  426.         {
  427.         m_pIMalloc->Release();
  428.         m_pIMalloc=NULL;
  429.         }
  430.  
  431.     return;
  432.     }
  433.